function Util() { }

Util.prototype = {
    maxBookmarkNumber: 10, //一度に表示るすブックマークリストの数 +1
    itemByteNumber: 66, //商品名の表示バイト数
    isShowSpinner: false,
    SNAPPER_CLOSED_STATE: 'closed',
    serverConnecting: false,
    isEditMode: false,
    
    showSpinner: function(message){
        if (Util.prototype.isShowSpinner) {
            return false;
        }
        Util.prototype.isShowSpinner = true;
        var title = (typeof message != 'undefined' && message != null) ? message : '読み込み中...'; 
        var options = {
            title: title,
            titleColor: '#4169E1'
        };
        monaca.showSpinner(options);

        return true;
    },
    
    hideSpinner: function(){
        if (Util.prototype.isShowSpinner) {
            monaca.hideSpinner();
            Util.prototype.isShowSpinner = false;
            return true;
        } else {
            return false;
        }
    },
    
    
    /**
     * @author: 小林
     * @description : 画面最上部へスクロール
     * @return string
     */
    scrollTop: function(){
        $('html,body').animate({ scrollTop: 0 }, 1);
    },
    
    /**
     * @author: 小林
     * @description : x日前の日付を求める
     * @return string
     */
    addDays: function(xDays){
        var dt = new Date(),
            baseSec = dt.getTime(),
            addSec = xDays * 86400000, //日数 * 1日のミリ秒数
            targetSec = baseSec + addSec;
        dt.setTime(targetSec);
        var XAgoYMD = Util.prototype.createYMD(dt);

        return XAgoYMD;
    },
    
    getTodayObj: function() {
        var today = new Date();
        var year = today.getFullYear();
        var month = today.getMonth() + 1;
        if (month < 10) {
            month = '0' + month;
        }
        var date = today.getDate();
        if (date < 10) {
            date = '0' + date;
        }
        var hour = today.getHours();
        if (hour < 10) {
            hour = '0' + hour;
        }
        var minute = today.getMinutes();
        if (minute < 10) {
            minute = '0' + minute;
        }
        var second = today.getSeconds();
        if (second < 10) {
            second = '0' + second;
        }
        var hms =   +  '' + today.getMinutes() + today.getSeconds();
        return {"ymd" : year + '' + month + '' + date, "y" : year, "m" : month, "d" : date,
                "hms" : hour + '' + minute + '' + second};
    },
    
    /**
     * @author: 小林
     * @description : yyyy/mm/ddを作成する
     * @return string
     */
    createYMD: function(dt){
        var year = dt.getFullYear(),
            month = dt.getMonth() + 1,
            date = dt.getDate();
        if(month < 10){
            month = "0" + month.toString(); 
        } 
        if(date < 10){
            date = "0" + date.toString(); 
        }    
        var ymd = year.toString() +'/'+ month.toString() +'/'+ date.toString();
        
        return ymd;
    },
    
    getNumberOfDays: function(year, month) {
        var isLeap = ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0));
        return [31, (isLeap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1];
    },
    
    /**
     * @author: 小林
     * @description : 数値3桁ずつカンマ区切りにする
     * @return string
     */
    addComma: function(num){
        num = String( num ).replace( /(\d)(?=(\d\d\d)+(?!\d))/g, '$1,' );
        return num;
    }, 
    
    /**
     * @author: 小林
     * @description : カンマを除去する
     * @return string
     */
    removeComma: function(num){
        num = String( num ).replace( /,/g, '' );
        //num = num.split(",").join("");
        return num;
    }, 
    
    /**
     * @author: 小林
     * @description : 全角から半角に変換する
     * @return string
     */
    getHalfChar: function(str){
        return str.replace(/[Ａ-Ｚａ-ｚ０-９]/g, function(s) {
            return String.fromCharCode(s.charCodeAt(0) - 65248);
        });
    }, 
    
    /**
     * @author: 小林
     * @description : 1～999,999,999の整数かどうか判定する
     * @return string
     */
    checkNumber: function(str){
        var check;
        if((str.match(/^-?[0-9]+$/) && str > 0 && str < 999999999) || str == ""){
            check = "true";
        }else{
            check = "false";
        }
        return check;
    }, 
    
    /**
     * @author: 小林
     * @description : 奇数か偶数か判定する、themeを変える
     * @return string
     */
    checkOddEven: function(num){
        var check;
        num = parseInt(num, 10);
        if (num % 2 == 0) {check = "even";}//偶数
        else {check = "odd";}//奇数
        return check;
    }, 
    
    /**
     * @author: 小林
     * @description : ○バイト以降を省略した文字列を生成する
     * @return string
     */
    getTextByByte: function(text){
        var count = 0;
        var half = 0;
        for (i=0; i<text.length; i++){
            n = escape(text.charAt(i));
            if (n.length < 4){
                count++; 
                half++;
            }else count+=2;
            
            if (count >= Util.prototype.itemByteNumber &&  text.length != Util.prototype.itemByteNumber / 2){
                text = text.slice(0, Util.prototype.itemByteNumber / 2 + half) + "…";
                break;
            }else if (count == Util.prototype.itemByteNumber && text.length == Util.prototype.itemByteNumber / 2){
                break;
            }
        }
        
        var count = 0;
        for (i=0; i<text.length; i++){
            n = escape(text.charAt(i));
            if (n.length < 4) count++; else count+=2;
        }
//        console.log(count);
        return text;
    },
    
    getSnapperState: function() {
        return snapper.state().state;  
    },
    
    isSnapperClosed: function() {
        return (snapper.state().state == Util.prototype.SNAPPER_CLOSED_STATE);
    },
    
    changeSnapperElement: function(pageContentId, disable) {        
        snapper = new Snap({
                element: document.getElementById(pageContentId),
                disable: 'right',
                touchToDrag: ((typeof disable != "undefined" && disable) ? false : true)
        });
        // if (typeof disable != "undefined" && disable) {
        //     snapper.disable();
        // }
        
//        snapper.disable();
//        snap.js - line 472
        snapper.close();
    }, 
    
    getDeviceWidth: function() {
        return window.innerWidth;
    }, 
    
    getDeviceHeight: function() {
        return window.innerHeight;
    }, 
    
    getDeviceSize: function() {
        return {width: window.innerWidth, height: window.innerHeight};
    },
    
    getMapWidth: function() {
        return (window.innerWidth - 0);
    },
    
    currentCityName: null,
    
    /**
     * 編集アイコン表示(ブックマーク一覧モード)
     */
    showEditIcon: function(name){
        monaca.updateUIStyle("editfooter", "visibility", "true");
        monaca.updateUIStyle("canselfooter", "visibility", "false");        
        monaca.updateUIStyle("deletefooter", "visibility", "false");
        monaca.updateUIStyle("deletefooter", "disable", "true");
        monaca.updateUIStyle("checkallfooter", "text", "すべて選択");
        monaca.updateUIStyle("checkallfooter", "visibility", "false");
        
        if (name !== null && typeof name !== 'undefined'){
            Util.prototype.currentCityName = name;
            monaca.updateUIStyle("mapfooter", "visibility", "true");
        } else {
            monaca.updateUIStyle("mapfooter", "visibility", "false");
            Util.prototype.currentCityName = null;
        }
        
        $(".deleteList").hide();
        $(".editList").hide();
        $("[name='deleteCheck']:checked").prop('checked', false).checkboxradio("refresh");
        
        Util.prototype.isEditMode = false;
        Util.prototype.refreshPageScroller();
    },
    
    /**
     * 削除ボタン、編集ボタン表示(ブックマーク編集モード)
     */
    showDeleteEditButton: function(name){
        monaca.updateUIStyle("editfooter", "visibility", "false");
        monaca.updateUIStyle("canselfooter", "visibility", "true");
        monaca.updateUIStyle("deletefooter", "visibility", "true");
        monaca.updateUIStyle("checkallfooter", "visibility", "true");
        monaca.updateUIStyle("mapfooter", "visibility", "false");
        
        $(".deleteList").show();
        $(".editList").css("display", "inline-block");
        Util.prototype.isEditMode = true;
        Util.prototype.refreshPageScroller();
    },
    
    refreshPageScroller: function() {
        $("." + $.mobile.activePage.attr("id") + "-content").jqmData("iscrollview").refresh();
    },
    
    checkInternetConnection: function() {
        var networkStt = navigator.connection.type;
        return ((networkStt === 'none' || networkStt === 'unknown'));
    },
    
    showInternetAlert: function() {
        var networkStt = navigator.connection.type;
        if (networkStt === 'none' || networkStt === 'unknown') {            
            Util.prototype.alertInternet();
            return false;
        } else {
            return true;
        }
    },
    
    alertInternet: function() {
        Util.prototype.hideSpinner();
        navigator.notification.confirm("インターネットが接続できません！Wifiまたはモバイルインターネットはオフに設定していませんか？", function(buttonIndex){
            if (buttonIndex == 1) {
                PageChangeController.prototype.changeToPage('pageHome');
            }
        }, "インターネットエラー", ["OK"]);
    }
};
